package com.lethelh.lambda;

import java.util.Random;
import java.util.function.Function;

/**
 * 利用Function函数式接口生成定长随机字符串
 */
public class FunctionSample {
    public static void main(String[] args) {
        Function<Integer, String> randomStringFunction = size -> {
          String chars = "abcdefghijklmnopqrstuvwxyz0123456789";
          StringBuffer sb  = new StringBuffer();
          Random random = new Random();
          for (int i = 0; i < size; i++) {
                int pos = random.nextInt(chars.length());
                sb.append(chars.charAt(pos));
          }
          return sb.toString();
        };
        String s = randomStringFunction.apply(6);
        System.out.println(s);
    }
}
